package com.ruoyi.gxdj.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gxdj.Vo.IdentityVo;
import com.ruoyi.gxdj.domain.InfoIdentityUp;
import com.ruoyi.gxdj.service.IInfoIdentityUpService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 身份晋升Controller
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/wechat/InfoIdentityUp")
public class WeChatInfoIdentityUpController extends BaseController
{
    @Autowired
    private IInfoIdentityUpService infoIdentityUpService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private HttpServletRequest request;
    /**
     * 查询身份晋升列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InfoIdentityUp infoIdentityUp)
    {
        startPage();
        List<IdentityVo> list = infoIdentityUpService.selectInfoIdentityUpListAll(infoIdentityUp);
        return getDataTable(list);
    }

    @GetMapping("/listUserByUser")
    public AjaxResult listlistUserById()
    {
        String taken = request.getHeader("taken");
        if (StringUtils.isNull(taken)){
            return AjaxResult.error("请先登录!");
        }
        SysUser user = iSysUserService.selectUserByOpenId(taken);
        List<InfoIdentityUp> list = infoIdentityUpService.listlistUserById(user.getUserId());
        return AjaxResult.success(list);
    }

    /**
     * 导出身份晋升列表
     */
    @Log(title = "身份晋升", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoIdentityUp infoIdentityUp)
    {
        List<InfoIdentityUp> list = infoIdentityUpService.selectInfoIdentityUpList(infoIdentityUp);
        ExcelUtil<InfoIdentityUp> util = new ExcelUtil<InfoIdentityUp>(InfoIdentityUp.class);
        util.exportExcel(response, list, "身份晋升数据");
    }

    /**
     * 获取身份晋升详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(infoIdentityUpService.selectInfoIdentityUpById(id));
    }

    /**
     * 新增身份晋升
     */
    @PostMapping
    public AjaxResult add(@RequestBody InfoIdentityUp infoIdentityUp)
    {
        String taken = request.getHeader("taken");
        SysUser user = iSysUserService.selectUserByOpenId(taken);
        infoIdentityUp.setUserId(user.getUserId());
        String createBy = infoIdentityUp.getCreateBy();
        user.setUserName(createBy);
        InfoIdentityUp infoIdentityUp1 =infoIdentityUpService.selectInfoIdentityUpByUser(user.getUserId());
        if (StringUtils.isNotNull(infoIdentityUp1)){
            return AjaxResult.error("审核通过后再提交！");

        }
        if (StringUtils.isNull(infoIdentityUp1)){
            iSysUserService.updateUser(user);
        }
        return toAjax(infoIdentityUpService.insertInfoIdentityUp(infoIdentityUp));
    }

    /**
     * 修改身份晋升
     */
    @Log(title = "身份晋升", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoIdentityUp infoIdentityUp)
    {

        return toAjax(infoIdentityUpService.updateInfoIdentityUp(infoIdentityUp));
    }

    /**
     * 删除身份晋升
     */
    @Log(title = "身份晋升", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoIdentityUpService.deleteInfoIdentityUpByIds(ids));
    }
}
